import { observable, action, toJS } from 'mobx';
import * as Api from '../../api/MyReportApi'; 

export default class BlogReportStore {
  @observable isLoading = true;
  @observable hasInfos = null;

  @observable tableHeader = [];
  @observable tableBody = [];
  @observable table = []; //拼接后完整table
  @observable attentionReport = []; //关注报表统计视图展示信息
  @observable detailReportHead = [];  //明细视图头部展示信息
  @observable detailReportBody = [];  //明细视图信息
  @observable detailReportLeft = [];  //明细视图左部展示信息
  @observable detailReport = []; //明细视图展示信息
  @observable detailWidth = 0;  // 明细视图长度

  @observable hasInfosDef = null;
  @observable tableHeaderDef = [];
  @observable tableBodyDef = [];
  @observable tableDef = []; //拼接后完整table
  @observable attentionReportDef = []; //关注报表统计视图展示信息
  @observable detailReportHeadDef = [];  //明细视图头部展示信息
  @observable detailReportBodyDef = [];  //明细视图信息
  @observable detailReportLeftDef = [];  //明细视图左部展示信息
  @observable detailReportDef = []; //明细视图展示信息
  @observable detailWidthDef = 0;  // 明细视图长度

  @observable reportList = {};
  // 我的报表
  getMyBlogReport = ( params = {} ) => {
    this.isLoading = true;
    Api.getMyBlogReport(params).then(data=>{
      this.tableHeader  = data.tableInfo.tableHeader;
      // 数组转置
      let arr = data.tableInfo.tableBody;
      this.tableBody = arr[0].map(function(col, i) {
        return arr.map(function(row) {
          return row[i];
       });
      });
      // 数组拼接
      this.table = toJS(this.tableBody).map((item,key)=>{
        let a = item.slice(0);
        a.unshift(data.tableInfo.tableHeader[key]);
        return a;
      });
      this.isLoading = false;
    });
  }
  getMyBlogReportList = () => {
    Api.getMyBlogReportList().then(data=>{///api/blog/report/getMyBlogReportList
      this.reportList = data.reportList;
    })
  }

  @action('关注报表')
  getAttentionReport = ( params = {} ) => {
    this.isLoading = true;
    Api.getAttentionReport(params).then(data=>{
      this.hasInfos = data.hasInfos;
      this.detailReportHead = [];
      this.detailReportBody = [];
      this.detailReportLeft = [];
      this.detailReport = [];
      this.detailWidth = 0;

      let detailHead = data.tableInfo.tableHeader;
      detailHead.splice(detailHead.length-2)
      detailHead.shift()
      this.detailReportHead = detailHead;
      this.detailWidth = detailHead.length;
      let arr = data.tableInfo.tableBody;
      
      // 删除最后一个元素并返回数组(统计视图信息)
      arr.pop();
      this.attentionReport = arr;

      arr.map((item,i) => {
        this.detailReportLeft.push(item[0])
        item.splice(item.length-2)
        this.detailReportBody.push(item)

        item.shift()
        this.detailReport.push(item)
      })
      this.isLoading = false;
    })
  }

  @action('自定义报表')
  getDefReport = ( params = {} ) => {
    this.isLoading = true;
   
    Api.getDefReport(params).then(data=>{
      this.hasInfosDef = data.hasInfos;
      this.detailReportHeadDef = [];
      this.detailReportBodyDef = [];
      this.detailReportLeftDef = [];
      this.detailReportDef = [];
      this.detailWidthDef = 0;

      let detailHeadDef = data.tableInfo.tableHeader;
      detailHeadDef.splice(detailHeadDef.length-2)
      detailHeadDef.shift()
      this.detailReportHeadDef = detailHeadDef;
      this.detailWidthDef = detailHeadDef.length;
      let arr = data.tableInfo.tableBody;
      
      // 删除最后一个元素并返回数组(统计视图信息)
      //arr.pop();
      this.attentionReportDef = arr;

      arr.map((item,i) => {
        this.detailReportLeftDef.push(item[0])
        item.splice(item.length-2)
        this.detailReportBodyDef.push(item)

        item.shift()
        this.detailReportDef.push(item)
      })
      this.isLoading = false;
    })
  }
}

